<template>
  <basicModal
    @register="modalRegister"
    ref="modalRef"
    class="basicModal basicFormModal"
    @on-ok="handleSubmit"
    @on-close="handleClose"
  >
    <template #default>
      <n-form
        class="ls-form"
        ref="formRef"
        :model="formData"
        label-placement="left"
        label-width="85px"
      >
    <#list table.fields as field>
      <#if field.propertyName != 'id' 
        && field.propertyName != 'createUser' 
        && field.propertyName != 'createTime' 
        && field.propertyName != 'updateUser' 
        && field.propertyName != 'updateTime' 
        && field.propertyName != 'delFlag' 
        && field.propertyName != 'tenantId'
      >
      <#if (field.customMap?? && field.customMap?size > 0)>
        <#if (field.customMap?size > 2)>
        <n-form-item
          label="${field.comment}"
          path="${field.propertyName}"
          :rule="{ type: 'number', required: true, message: '请选择${field.comment}', trigger: 'change' }"
        >
          <n-select
            v-model:value="formData.${field.propertyName}"
            placeholder="请选择${field.comment}"
            :options="optionData.${field.propertyName}List"
          />
        </n-form-item>
        <#else>
        <n-form-item label="${field.comment}" path="${field.propertyName}">
          <n-radio-group v-model:value="formData.${field.propertyName}" path="${field.propertyName}">
          <#list field.customMap?keys as key>
            <n-radio :value="${key}">${field.customMap[key]!}</n-radio>
          </#list>
          </n-radio-group>
        </n-form-item>
        </#if>
      <#elseif field.propertyName == 'cover' || field.propertyName == 'logo' || field.propertyName == 'image'>
        <n-form-item
          label="${field.comment}"
          path="${field.propertyName}"
          :rule="{ required: true, message: '请上传${field.comment}', trigger: 'change' }"
        >
          <UploadImg
            @change-file-name="(name) => (formData.imageImgName = name)"
            :fileType="['image/jpeg', 'image/png', 'image/jpg', 'image/gif']"
            name="${entity?lower_case}"
            :fileSize="200"
            v-model:image-url="formData.${field.propertyName}"
            :cropper="false"
          >
            <template #tip>支持扩展名: jpg png jpeg;文件大小不超过200M</template>
          </UploadImg>
        </n-form-item>
      <#elseif field.columnType?string == 'INTEGER'>
        <n-form-item label="${field.comment}" path="${field.propertyName}">
          <n-input-number v-model:value="formData.${field.propertyName}" />
        </n-form-item>
      <#elseif field.columnType?string == 'LOCAL_DATE_TIME'>
        <n-form-item
          label="${field.comment}"
          path="${field.propertyName}"
          :rule="{ type: 'date', required: true, message: '请选择${field.comment}', trigger: 'change' }"
        >
          <n-date-picker
            type="datetime"
            placeholder="请选择${field.comment}"
            v-model:value="formData.${field.propertyName}"
            format="yyyy-MM-dd HH:mm:ss"
            valueFormat="yyyy-MM-dd HH:mm:ss"
            clearable
          />
        </n-form-item>
      <#elseif field.columnType?string == 'LOCAL_DATE'>
        <n-form-item
          label="${field.comment}"
          path="${field.propertyName}"
          :rule="{ type: 'date', required: true, message: '请选择${field.comment}', trigger: 'change' }"
        >
          <n-date-picker
            type="date"
            placeholder="请选择${field.comment}"
            v-model:value="formData.${field.propertyName}"
            format="yyyy-MM-dd"
            valueFormat="yyyy-MM-dd"
            clearable
          />
        </n-form-item>
      <#else>
      <n-form-item
          label="${field.comment}"
          path="${field.propertyName}"
          :rule="{ required: true, message: '请输入${field.comment}', trigger: 'blur' }"
        >
          <n-input v-model:value="formData.${field.propertyName}" placeholder="请输入${field.comment}" clearable />
        </n-form-item>
      </#if>
      </#if>
    </#list>
      </n-form>
    </template>
  </basicModal>
</template>
<script lang="ts" setup>
  import { get${entity}Detail, ${entity?lower_case}Add, ${entity?lower_case}Update } from '@/api/tool/${entity?lower_case}';
  import { onMounted, reactive, ref } from 'vue';
  import { useMessage } from 'naive-ui';
  import UploadImg from '@/components/Upload/Image.vue';
  import { useModal } from '@/components/Modal';

  /**
   * 定义参数
   */
  const emit = defineEmits(['success', 'update:visible']);
  const formRef = ref();

  /**
   * 定义表单参数
   */
  const message = useMessage();
  const formData = reactive({
    id: '',
<#list table.fields as field>
  <#if field.propertyName != 'id' 
    && field.propertyName != 'createUser' 
    && field.propertyName != 'createTime' 
    && field.propertyName != 'updateUser' 
    && field.propertyName != 'updateTime' 
    && field.propertyName != 'delFlag' 
    && field.propertyName != 'tenantId'
  >
  <#if (field.customMap?? && field.customMap?size > 0)>
    ${field.propertyName}: ${field.metaInfo.defaultValue},
  <#elseif field.columnType?string == 'INTEGER'>
    ${field.propertyName}: ${field.metaInfo.defaultValue!0},
  <#elseif field.columnType?string == 'LOCAL_DATE_TIME' || field.columnType?string == 'LOCAL_DATE'>
    ${field.propertyName}: null,
  <#else>
    ${field.propertyName}: '',
  </#if>
  </#if>
</#list>
  });

  /**
   * 定义接收的参数
   */
  const props = defineProps({
    visible: {
      type: Boolean,
      required: true,
      default: false,
    },
    ${entity?lower_case}Id: {
      type: Number,
      required: true,
      default: 0,
    },
  });

  /**
   * 定义数据源
   */
  const optionData = {
<#list table.fields as field>
  <#if (field.customMap?? && field.customMap?size > 0)>
    <#if (field.customMap?size > 2)>
    /**
     * 定义${field.comment}数据源
    */
    ${field.propertyName}List: [
    <#list field.customMap?keys as key>
      { label: '${field.customMap[key]!}', value: ${key} },
    </#list>
    ],
    </#if>
  </#if>
</#list>
  };

  /**
   * 定义模态
   */
  const [modalRegister, { openModal, setSubLoading }] = useModal({
    title: props.${entity?lower_case}Id ? '编辑${table.comment!}' : '添加${table.comment!}',
    subBtuText: '确定',
    width: 600,
  });

  /**
   * 执行提交
   */
  const handleSubmit = async () => {
    formRef.value
      .validate()
      .then(async () => {
        props.${entity?lower_case}Id ? await ${entity?lower_case}Update(formData) : await ${entity?lower_case}Add(formData);
        message.success('操作成功');
        setSubLoading(false);
        emit('update:visible', false);
        emit('success');
      })
      .catch((error) => {
        setSubLoading(false);
      });
  };

  /**
   * 关闭窗体
   */
  const handleClose = () => {
    emit('update:visible', false);
  };

  /**
   * 设置表单数据
   */
  const setFormData = async () => {
    const data = await get${entity}Detail(props.${entity?lower_case}Id);
    for (const key in formData) {
      if (data[key] != null && data[key] != undefined) {
        //@ts-ignore
        formData[key] = data[key];
      }
    }
  };

  /**
   * 钩子函数
   */
  onMounted(() => {
    if (props.${entity?lower_case}Id) {
      setFormData();
    }
  });

  /**
   * 定义函数
   */
  defineExpose({
    openModal,
  });
</script>
